/*
 * @Description: 
 * @Author: HKH
 * @Date: 2024-10-11 19:05:12
 * @LastEditors: HKH
 * @LastEditTime: 2024-10-11 19:05:33
 */
public class Printer {

    // 普通（非静态）的同步方法
    // 普通同步方法的锁：默认的锁对象是 this
    public synchronized void print1() {
      System.out.print("天");
      System.out.print("天");
      System.out.print("向");
      System.out.print("上");
      System.out.println("");
    }
  
    Object mutex = new Object();
  
    // 不同步的方法
    public void print2() {
      // 同步代码块 - 要和同步方法使用同一把锁
      synchronized (this) {
        System.out.print("好");
        System.out.print("好");
        System.out.print("学");
        System.out.print("习");
        System.out.println("");
      }
    }
  
    // 静态同步方法 - 锁对象是 Printer.class
    public static synchronized void print3() {
      System.out.print("天");
      System.out.print("天");
      System.out.print("向");
      System.out.print("上");
      System.out.println("");
    }
  
    public void print4() {
      // 同步代码块 - 要和同步方法使用同一把锁
      synchronized (Printer.class) {
        System.out.print("好");
        System.out.print("好");
        System.out.print("学");
        System.out.print("习");
        System.out.println("");
      }
    }
  }